import { Injectable } from '@angular/core';
import { IPush } from './ipush.interface';
import { TagProtocol } from './tag-protocol';
import { Subject } from 'rxjs/Subject';
import { TagData } from '../../entity/tag-data.model';
import { FaultData } from '../../entity/fault-data';
import { ApplicationService } from '../../service/application.service';
/**
 * 数据模拟
 *
 * @export
 * @class MockPushService
 * @implements {IPush}
 */
@Injectable()
export class MockPushService implements IPush {
    /**
     * 主题对象数组
     *
     * @private
     * @type {Subject<any>[]}
     * @memberof IMPushService
     */
    private subjectMap: Subject<any>[] = [];
    private timers: any[] = [];
    constructor(
      private application?: ApplicationService
    ) {
        let self = this;
        window.onbeforeunload = function () {
            self.close();
        }
    }
    begin(protocol: TagProtocol): Subject<TagData[]> {
        let subject = new Subject<TagData[]>();
        this.subjectMap.push(subject);
        let datas: TagData[] = [];
        let timer = setInterval(o => {
            let datas: TagData[] = [];
            protocol.tagArrary.forEach(tagCode => {
                let data: TagData = {
                    code: tagCode,
                    value: 0,
                    time: new Date(),
                }
                if (tagCode.toUpperCase().endsWith('TurbineStatus'.toUpperCase())) {
                    data.value = Math.floor(Math.random() * 5) + 1;
                }
                else if (tagCode.toUpperCase().endsWith('OpeStaInt'.toUpperCase())) {
                    data.value = Math.floor(Math.random() * 5) + 1;
                }
                else if (tagCode.toUpperCase().endsWith('RunCount'.toUpperCase())) {
                    data.value = Math.floor(Math.random() * 100);
                }
                else if (tagCode.toUpperCase().endsWith('BreakdownCount'.toUpperCase())) {
                    data.value = Math.floor(Math.random() * 100);
                }
                else if (tagCode.toUpperCase().endsWith('StopCount'.toUpperCase())) {
                    data.value = Math.floor(Math.random() * 100);
                }
                else if (tagCode.toUpperCase().endsWith('NoComCount'.toUpperCase())) {
                    data.value = Math.floor(Math.random() * 100);
                }
                else if (tagCode.toUpperCase().endsWith('ServiceCount'.toUpperCase())) {
                    data.value = Math.floor(Math.random() * 100);
                }
                else if (tagCode.toUpperCase().endsWith('PowerD'.toUpperCase())) {
                    data.value = Math.floor(Math.random() * 1000000);
                }
                else if (tagCode.toUpperCase().endsWith('GriReaPower'.toUpperCase())) {
                    data.value = 1;
                } else if (tagCode.toUpperCase().endsWith('PowerM'.toUpperCase())) {
                    data.value = Math.random() * 10000000145;
                } else if (tagCode.toUpperCase().endsWith('Power'.toUpperCase())) {
                    data.value = -6.1;
                }
                else {
                    data.value = Math.random() * 100;
                }

                datas.push(data);
            });
            subject.next(datas);
        }, 10000);
        this.timers.push(timer);
        return subject;
    }
    interval(protocol: TagProtocol, interval: number): Subject<TagData[]> {
        throw new Error('Method not implemented.');
    }
    withHistories(protocol: TagProtocol, start: Date, end: Date): Subject<any>;
    withHistories(protocol: TagProtocol, start: Date, end: Date, interval: number): Subject<any>;
    withHistories(protocol: TagProtocol, start: Date, end: Date, interval: { history: number; push: number; }): Subject<any>;
    withHistories(protocol: TagProtocol, start: Date, end: Date, interval?: any): Subject<any> {
        let subject = new Subject<TagData[]>();
        this.subjectMap.push(subject);
        let historyDatas: TagData[] = [];
        protocol.startTime = start;
        protocol.endTime = end;

        let history = 2 * 60;
        if (interval != null) {
            if (typeof interval == 'number') {
                history = interval;
            }
            else if (typeof interval == 'object') {
                history = interval.history;
            }
        }
        protocol.tagArrary.forEach(tagCode => {
            let tumDate = start;
            while (tumDate <= end) {
                tumDate = new Date(tumDate.getTime() + history * 1000);
                let tumTagData: TagData = {
                    code: tagCode,
                    value: Math.random() * 10,
                    time: tumDate,
                }
                historyDatas.push(tumTagData);
            }
        });
        setTimeout(o => {
            subject.next(historyDatas);
        }, 1000);

        if (interval != null) {
            if (typeof interval == 'number') {
                protocol.historyInterval = interval;
                protocol.setDefineProperty();
                Object.defineProperty(protocol, 'pushInterval', { enumerable: false });
                Object.defineProperty(protocol, 'historyInterval', { enumerable: true });

                let timer = setInterval(o => {
                    let datas: TagData[] = [];
                    protocol.tagArrary.forEach(tagCode => {
                        let data: TagData = {
                            code: tagCode,
                            value: Math.random() * 100,
                            time: new Date(),
                        }
                        datas.push(data);
                    })
                    subject.next(datas);
                }, 1500);
                this.timers.push(timer);
            }
            else if (typeof interval == 'object') {
                protocol.pushInterval = interval.push;
                protocol.historyInterval = interval.history;
                protocol.setDefineProperty();
                Object.defineProperty(protocol, 'historyInterval', { enumerable: true });
                Object.defineProperty(protocol, 'pushInterval', { enumerable: true });

                let timer = setInterval(o => {
                    let datas: TagData[] = [];
                    protocol.tagArrary.forEach(tagCode => {
                        let data: TagData = {
                            code: tagCode,
                            value: Math.random() * 100,
                            time: new Date(),
                        }
                        datas.push(data);
                    });
                    subject.next(datas);
                }, interval.push * 1000);
                this.timers.push(timer);
            }
        }
        else {
            protocol.setDefineProperty();
            Object.defineProperty(protocol, 'historyInterval', { enumerable: false });
            Object.defineProperty(protocol, 'pushInterval', { enumerable: false });
            let timer = setInterval(o => {
                let datas: TagData[] = [];
                protocol.tagArrary.forEach(tagCode => {
                    let data: TagData = {
                        code: tagCode,
                        value: Math.random() * 100,
                        time: new Date(),
                    }
                })
                subject.next(datas);
            }, 1500);
            this.timers.push(timer);
        }
        return subject;
    }
    matrix(matrix: { tagId: number; farmIds: number[]; }): Subject<any> {
        throw new Error('Method not implemented.');
    }
    alarmHistory(farmIds: number[]): Subject<any> {
        throw new Error('Method not implemented.');
    }
    alarm(userId: number): Subject<FaultData[]> {
        let subject = new Subject<FaultData[]>();
        this.subjectMap.push(subject);

        let timer = setInterval(() => {
          let index = parseInt((Math.random() * this.application.currentUserFarms.length).toString(), 10);

            let data11: FaultData = {
                deviceId: 4,
                deviceName: '#4',
                farmId: this.application.currentUserFarms[index].farmId,
                farmName: this.application.currentUserFarms[index].farmName,
                turbComponentId: 1,
                turbComponentName: '发电机系统',
                subcomponentName: '',
                partsName: '',
                faultCode: '728',
                faultDesc: '最低电压___V, Dip___毫秒',
                faultLevelId: 1,
                faultLevelName: '高',
                faultLevelColor: '#FC1829',
                faultTime: new Date(),
                faultEndTime: new Date(),
                alarmCategoryId: 1,
                alarmCategoryName: '报警',
                confirm: true
            };
            let data12: FaultData = {
                deviceId: 4,
                deviceName: '#4',
                farmId: this.application.currentUserFarms[index].farmId,
                farmName: this.application.currentUserFarms[index].farmName,
                turbComponentId: Math.floor(Math.random() * 3),
                turbComponentName: '发电机系统',
                subcomponentName: '',
                partsName: '',
                faultCode: '728',
                faultDesc: '最低电压___V, Dip___毫秒',
                faultLevelId: 2,
                faultLevelName: '中',
                faultLevelColor: '#FC1829',
                faultTime: new Date(),
                faultEndTime: new Date(),
                alarmCategoryId: 1,
                alarmCategoryName: '报警',
                confirm: true
            };
            let data13: FaultData = {
                deviceId: 4,
                deviceName: '#4',
                farmId: this.application.currentUserFarms[index].farmId,
                farmName: this.application.currentUserFarms[index].farmName,
                turbComponentId: Math.floor(Math.random() * 3),
                turbComponentName: '发电机系统',
                subcomponentName: '',
                partsName: '',
                faultCode: '728',
                faultDesc: '最低电压___V, Dip___毫秒',
                faultLevelId: 3,
                faultLevelName: '低',
                faultLevelColor: '#FC1829',
                faultEndTime: new Date(),
                faultTime: new Date(),
                alarmCategoryId: 1,
                alarmCategoryName: '报警',
                confirm: true
            };
            let faultDatas11: FaultData[] = [data11, data12, data13];
            let data21: FaultData = {
                deviceId: 4,
                deviceName: '#4',
                farmId: this.application.currentUserFarms[index].farmId,
                farmName: this.application.currentUserFarms[index].farmName,
                turbComponentId: 1,
                turbComponentName: '发电机系统',
                subcomponentName: '',
                partsName: '',
                faultCode: '728',
                faultDesc: '最低电压___V, Dip___毫秒',
                faultLevelId: 2,
                faultLevelName: '中',
                faultLevelColor: '#FC1829',
                faultEndTime: new Date(),
                faultTime: new Date(),
                alarmCategoryId: 2,
                alarmCategoryName: '告警',
                confirm: true
            };
            let data22: FaultData = {
                deviceId: 4,
                deviceName: '#4',
                farmId: this.application.currentUserFarms[index].farmId,
                farmName: this.application.currentUserFarms[index].farmName,
                turbComponentId: Math.floor(Math.random() * 3),
                turbComponentName: '发电机系统',
                subcomponentName: '',
                partsName: '',
                faultCode: '728',
                faultDesc: '最低电压___V, Dip___毫秒',
                faultLevelId: 2,
                faultLevelName: '中',
                faultLevelColor: '#FC1829',
                faultEndTime: new Date(),
                faultTime: new Date(),
                alarmCategoryId: 2,
                alarmCategoryName: '告警',
                confirm: true
            };
            let data23: FaultData = {
                deviceId: 4,
                deviceName: '#4',
                farmId: this.application.currentUserFarms[index].farmId,
                farmName: this.application.currentUserFarms[index].farmName,
                turbComponentId: Math.floor(Math.random() * 3),
                turbComponentName: '发电机系统',
                subcomponentName: '',
                partsName: '',
                faultCode: '728',
                faultDesc: '最低电压___V, Dip___毫秒',
                faultLevelId: 2,
                faultLevelName: '中',
                faultLevelColor: '#FC1829',
                faultEndTime: new Date(),
                faultTime: new Date(),
                alarmCategoryId: 2,
                alarmCategoryName: '告警',
                confirm: true
            };
            faultDatas11.push(data21);
            faultDatas11.push(data22);
            faultDatas11.push(data23);
            faultDatas11.map(o => {
                o.faultTime = new Date();
                o.confirm = false;
            });
            subject.next(faultDatas11);
        }, 10 * 1000);
        this.timers.push(timer);
        return subject;
    }

    substation(userId: number): Subject<FaultData[]> {
        let subject = new Subject<FaultData[]>();
        this.subjectMap.push(subject);
        return subject;
    }


    close() {
        this.subjectMap.forEach(map => {
            map.unsubscribe();
        });
        this.timers.forEach(timer => {
            clearInterval(timer);
        })
    }



}



// WEBPACK FOOTER //
// ./src/app/service/data-source/mock-push.service.ts